package com.srwl.mytx.im;

import com.alibaba.fastjson.JSONObject;
import com.srwl.mytx.Constant;
import com.srwl.mytx.domain.Message;
import com.srwl.mytx.im.protobuf.MessageProtobuf;
import com.srwl.mytx.utils.StringUtil;


import java.util.UUID;


/**
 * <p>@ProjectName:     BoChat</p>
 * <p>@ClassName:       MessageBuilder.java</p>
 * <p>@PackageName:     com.bochat.app.message</p>
 * <b>
 * <p>@Description:     消息转换</p>
 * </b>
 * <p>@author:          FreddyChen</p>
 * <p>@date:            2019/02/07 17:26</p>
 * <p>@email:           chenshichao@outlook.com</p>
 */
public class MessageBuilder {

    /**
     * 根据聊天消息，生成一条可以能够传输通讯的消息
     *
     * @param msgId
     * @param type
     * @param subType
     * @param fromId
     * @param toId
     * @param extend
     * @param content
     * @return
     */
//    public static AppMessage buildAppMessage(String msgId, int type, int subType, String fromId,
//                                             String toId, String extend, String content) {
//        AppMessage message = new AppMessage();
//        Head head = new Head();
//        head.setMsgId(msgId);
//        head.setMsgType(type);
//        head.setMsgContentType(subType);
//        head.setFromId(fromId);
//        head.setToId(toId);
//        head.setExtend(extend);
//        message.setHead(head);
//        message.setBody(content);
//
//        return message;
//    }

    /**
     * 根据聊天消息，生成一条可以能够传输通讯的消息
     *
     * @param msg
     * @return
     */
//    public static AppMessage buildAppMessage(ContentMessage msg) {
//        AppMessage message = new AppMessage();
//        Head head = new Head();
//        head.setMsgId(msg.getMsgId());
//        head.setMsgType(msg.getMsgType());
//        head.setMsgContentType(msg.getMsgContentType());
//        head.setFromId(msg.getFromId());
//        head.setToId(msg.getToId());
//        head.setTimestamp(msg.getTimestamp());
//        head.setExtend(msg.getExtend());
//        message.setHead(head);
//        message.setBody(msg.getContent());
//
//        return message;
//    }

    /**
     * 根据聊天消息，生成一条可以能够传输通讯的消息
     *
     * @param msg
     * @return
     */
//    public static AppMessage buildAppMessage(BaseMessage msg) {
//        AppMessage message = new AppMessage();
//        Head head = new Head();
//        head.setMsgId(msg.getMsgId());
//        head.setMsgType(msg.getMsgType());
//        head.setMsgContentType(msg.getMsgContentType());
//        head.setFromId(msg.getFromId());
//        head.setToId(msg.getToId());
//        head.setExtend(msg.getExtend());
//        head.setTimestamp(msg.getTimestamp());
//        message.setHead(head);
//        message.setBody(msg.getContent());
//
//        return message;
//    }

    /**
     * 根据业务消息对象获取protoBuf消息对应的builder
     *
     * @param message
     * @return
     */
    public static MessageProtobuf.Msg.Builder getProtoBufMessageBuilderByAppMessage(Message message,int msgType) {
        MessageProtobuf.Msg.Builder builder = MessageProtobuf.Msg.newBuilder();
        MessageProtobuf.Head.Builder headBuilder = MessageProtobuf.Head.newBuilder();
        headBuilder.setMsgType(msgType);
        // headBuilder.setStatusReport(message.getHead().getStatusReport());
        //  headBuilder.setMsgContentType(message.getHead().getMsgContentType());
        if (!StringUtil.isEmpty(message.getId()))
            headBuilder.setMsgId(message.getId());
        if (!StringUtil.isEmpty(message.getFromId()))
            headBuilder.setFromId(message.getFromId());
        if (!StringUtil.isEmpty(message.getToId()))
            headBuilder.setToId(message.getToId());
        if (message.getCreated() != null)
            headBuilder.setTimestamp(message.getCreated().getTime());
//        if (!StringUtil.isEmpty(message.getExtraData())) 附加信息方message 里面携带发送，没必要方head 里面
//            headBuilder.setExtend(message.getExtraData());

        builder.setBody(JSONObject.toJSONString(message));
        builder.setHead(headBuilder);
        return builder;
    }


    /**
     * 构建客户端消息接收状态报告
     *
     * @param msgId
     * @return
     */
    public static MessageProtobuf.Msg buildReceivedReportMsg(String msgId) {
        if (StringUtil.isEmpty(msgId)) {
            return null;
        }

        MessageProtobuf.Msg.Builder builder = MessageProtobuf.Msg.newBuilder();
        MessageProtobuf.Head.Builder headBuilder = MessageProtobuf.Head.newBuilder();
        headBuilder.setMsgId(UUID.randomUUID().toString());
        headBuilder.setMsgType(Constant.IM_TYPE_RECEIVED_ACK);
        headBuilder.setTimestamp(System.currentTimeMillis());
//        JSONObject jsonObj = new JSONObject();
//        jsonObj.put("msgId", msgId);
        headBuilder.setExtend(msgId);
        builder.setHead(headBuilder.build());

        return builder.build();
    }

    /**
     * 构建客户端消息接收状态报告
     *
     * @param fromId
     * @return
     */
    public static MessageProtobuf.Msg buildOffLineReceivedReportMsg(String fromId) {
        if (StringUtil.isEmpty(fromId)) {
            return null;
        }

        MessageProtobuf.Msg.Builder builder = MessageProtobuf.Msg.newBuilder();
        MessageProtobuf.Head.Builder headBuilder = MessageProtobuf.Head.newBuilder();
        headBuilder.setMsgId(UUID.randomUUID().toString());
        headBuilder.setMsgType(Constant.IM_TYPE_OFF_LINE_RECEIVED_ACK);
        headBuilder.setTimestamp(System.currentTimeMillis());
//        JSONObject jsonObj = new JSONObject();
//        jsonObj.put("msgId", msgId);
        headBuilder.setFromId(fromId);
        builder.setHead(headBuilder.build());

        return builder.build();
    }

    /**
     * 通过protobuf消息对象获取业务消息对象
     *
     * @param protobufMessage
     * @return
     */
//    public static AppMessage getMessageByProtobuf(
//            MessageProtobuf.Msg protobufMessage) {
//        AppMessage message = new AppMessage();
//        Head head = new Head();
//        MessageProtobuf.Head protoHead = protobufMessage.getHead();
//        head.setMsgType(protoHead.getMsgType());
//        head.setStatusReport(protoHead.getStatusReport());
//        head.setMsgContentType(protoHead.getMsgContentType());
//        head.setMsgId(protoHead.getMsgId());
//        head.setFromId(protoHead.getFromId());
//        head.setToId(protoHead.getToId());
//        head.setTimestamp(protoHead.getTimestamp());
//        head.setExtend(protoHead.getExtend());
//        message.setHead(head);
//        message.setBody(protobufMessage.getBody());
//        return message;
//    }
}
